package org.jeecg.modules.demo3.peijian.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.demo3.peijian.entity.AxhkDrCapacitance;
import org.jeecg.modules.demo3.peijian.entity.materials.Rental;
import org.jeecg.modules.demo3.peijian.service.IAxhkDrCapacitanceService;
import org.jeecg.modules.demo3.peijian.utill.PdfUtill;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Arrays;

 /**
 * @Description: 电容
 * @Author: jeecg-boot
 * @Date:   2021-04-13
 * @Version: V1.0
 */
@Api(tags="电容")
@RestController
@RequestMapping("/fly/axhkDrCapacitance")
@Slf4j
public class AxhkDrCapacitanceController extends JeecgController<AxhkDrCapacitance, IAxhkDrCapacitanceService> {
	@Autowired
	private IAxhkDrCapacitanceService axhkDrCapacitanceService;

	/**
	 * 分页列表查询
	 *
	 * @param axhkDrCapacitance
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "电容-分页列表查询")
	@ApiOperation(value="电容-分页列表查询", notes="电容-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(AxhkDrCapacitance axhkDrCapacitance,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AxhkDrCapacitance> queryWrapper = QueryGenerator.initQueryWrapper(axhkDrCapacitance, req.getParameterMap());
		Page<AxhkDrCapacitance> page = new Page<AxhkDrCapacitance>(pageNo, pageSize);
		IPage<AxhkDrCapacitance> pageList = axhkDrCapacitanceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param axhkDrCapacitance
	 * @return
	 */
	@AutoLog(value = "电容-添加")
	@ApiOperation(value="电容-添加", notes="电容-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody AxhkDrCapacitance axhkDrCapacitance) {
		axhkDrCapacitanceService.save(axhkDrCapacitance);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param axhkDrCapacitance
	 * @return
	 */
	@AutoLog(value = "电容-编辑")
	@ApiOperation(value="电容-编辑", notes="电容-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody AxhkDrCapacitance axhkDrCapacitance) {
		axhkDrCapacitanceService.updateById(axhkDrCapacitance);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "电容-通过id删除")
	@ApiOperation(value="电容-通过id删除", notes="电容-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		axhkDrCapacitanceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "电容-批量删除")
	@ApiOperation(value="电容-批量删除", notes="电容-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.axhkDrCapacitanceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "电容-通过id查询")
	@ApiOperation(value="电容-通过id查询", notes="电容-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		AxhkDrCapacitance axhkDrCapacitance = axhkDrCapacitanceService.getById(id);
		if(axhkDrCapacitance==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(axhkDrCapacitance);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param axhkDrCapacitance
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AxhkDrCapacitance axhkDrCapacitance) {
        return super.exportXls(request, axhkDrCapacitance, AxhkDrCapacitance.class, "电容");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AxhkDrCapacitance.class);
    }

	 //计算所有 现有数量 计算所有购买金额
	 @GetMapping("getByAxhkDrCapacitance")
	 public Result<?> getByAxhkDrCapacitance(){
		 Rental rental =  axhkDrCapacitanceService.AtAxhkDrCapacitance();
		 return Result.OK(rental);
	 }

	 //点击移除 根据id 删除数据 添加数据
	 @GetMapping("getByDrCapacitanceDate")
	 public Result<?> getByDrCapacitanceDate(String id,int num){
		 int i = axhkDrCapacitanceService.AtAxhkDrCapacitance(id,num);
		 if(i>0){
			 return Result.OK("成功！");
		 }
		 return Result.OK("不成功！");
	 }

	 //上传文件
	 @GetMapping("getByUpdateAxhkDrCapacitancePath")
	 public Result<?> getByUpdateAxhkDrCapacitancePath(String id,String names){
		 int i = axhkDrCapacitanceService.AtUpdateAxhkDrCapacitance(id,names);
		 return Result.OK(i);
    }

	 //点击删除 通过id 修改path 并且删除本地文件
	 @GetMapping("getByUpdatePath")
	 public Result<?> getByUpdatePath(String id){
		 int i= axhkDrCapacitanceService.AtuUpdateDrCapacitancePath(id);
		 return Result.OK(i);
	 }

	 @GetMapping("LookFile")
	 public Result<?> getLookFile(String id){
		 String filePath = axhkDrCapacitanceService.dataPath(id);
		 if(null != filePath){
			 return Result.OK(1);
		 }else{
			 return Result.OK(0);
		 }
	 }

	 //浏览器查看上传pdf文件
	 @GetMapping(value = "getByLookFile")
	 public void getByLookFile(@RequestParam(value = "id",required = false) String id, HttpServletResponse response){
		 InputStream inputStream = null;
		 OutputStream outputStream = null;
		 String filePath = axhkDrCapacitanceService.dataPath(id);
		 if(filePath != null && "" != filePath){
			 try {
				 File file = new File(filePath);
				 if (!file.exists()) {
					 response.setStatus(404);
					 throw new RuntimeException("文件不存在..");
				 }
				 response.setContentType("application/pdf");// 设置强制下载不打开
				 response.addHeader("Content-Disposition", "attachment;fileName=" + new String(file.getName().getBytes("UTF-8"), "iso-8859-1"));
				 inputStream = new BufferedInputStream(new FileInputStream(filePath));
				 outputStream = response.getOutputStream();
				 byte[] buf = new byte[inputStream.available()];
				 int len;
				 while ((len = inputStream.read(buf)) > 0) {
					 outputStream.write(buf, 0, len);
				 }
				 response.flushBuffer();
			 }catch (IOException e) {
				 log.error("预览文件失败" + e.getMessage());
				 response.setStatus(404);
				 e.printStackTrace();
			 } finally {
				 if (inputStream != null) {
					 try {
						 inputStream.close();
					 } catch (IOException e) {
						 log.error(e.getMessage(), e);
					 }
				 }
				 if (outputStream != null) {
					 try {
						 outputStream.close();
					 } catch (IOException e) {
						 log.error(e.getMessage(), e);
					 }
				 }
			 }
		 }
	 }

	 /*// pdf文件下载
	 @GetMapping("downloadFile")
	 public void downloadFile(@RequestParam(value = "id",required = false) String id, HttpServletResponse response){
		 String fileName = axhkDrCapacitanceService.dataPath(id);
		 if(null != fileName){
			 PdfUtill.downFile(fileName,response);
		 }
	 }*/


}
